package com.fedming.bottomnavigationdemo.Util;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;
import android.view.Gravity;
import android.widget.Toast;

import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;

public class NetWorkUtils {
    static String TAG = "NetWorkUtils";

    public static String getIPAddress(Context context) {
        NetworkInfo info = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
        if (info != null && info.isConnected()) {
            if (info.getType() == ConnectivityManager.TYPE_MOBILE) {//当前使用2G/3G/4G网络
                try {
                    //Enumeration<NetworkInterface> en=NetworkInterface.getNetworkInterfaces();
                    for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
                        NetworkInterface intf = en.nextElement();
                        for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                            InetAddress inetAddress = enumIpAddr.nextElement();
                            if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
                                return inetAddress.getHostAddress();
                            }
                        }
                    }
                } catch (SocketException e) {
                    e.printStackTrace();
                }

            } else if (info.getType() == ConnectivityManager.TYPE_WIFI) {//当前使用无线网络
                WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
                WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                String ipAddress = intIP2StringIP(wifiInfo.getIpAddress());//得到IPV4地址
                return ipAddress;
            }  else if(info.getType() == ConnectivityManager.TYPE_ETHERNET){
            String ipAddress = getHostIP();
            return ipAddress;
        }
        } else {
            //当前无网络连接,请在设置中打开网络
            Log.e(TAG,"本地IP获取失败，请检查网络链接");
            ((Activity)context).runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast toast = Toast.makeText(((Activity)context), "本地IP获取失败，请检查网络链接", Toast.LENGTH_SHORT);
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();
                }
            });
        }
        return null;
    }


    public static String intIP2StringIP(int ip) {
        return (ip & 0xFF) + "." +
                ((ip >> 8) & 0xFF) + "." +
                ((ip >> 16) & 0xFF) + "." +
                (ip >> 24 & 0xFF);
    }
    public static String getHostIP() {
        String hostIp = null;

        try {

            Enumeration nis = NetworkInterface.getNetworkInterfaces();

            InetAddress ia = null;

            while (nis.hasMoreElements()) {

                NetworkInterface ni = (NetworkInterface) nis.nextElement();

                Enumeration<InetAddress> ias = ni.getInetAddresses();

                while (ias.hasMoreElements()) {

                    ia = ias.nextElement();

                    if (ia instanceof Inet6Address) {

                        continue;// skip ipv6

                    }

                    String ip = ia.getHostAddress();

                    if (!"127.0.0.1".equals(ip)) {

                        hostIp = ia.getHostAddress();

                        break;

                    }

                }

            }

        } catch (SocketException e) {

            Log.i("yao", "SocketException");

            e.printStackTrace();

        }
        return hostIp;
    }
}
